<?php

class TImageUploader
{
    static $allTypes = array( 1 => 'gif', 2 => 'jpg', 3 => 'png' );
    
    // __construct
    function __construct( &$aFiles )
    {
        $this->allowedTypes  = array( 'gif', 'jpg', 'png' );
        $this->maxFileSize   = 600000;
        
        $this->files = $aFiles;
    }
    
    // validate
    function validate( &$aResultCode )
    {
        $aResultCode = 0;
        
        reset( $this->files );
        
        foreach( $this->files as $fileId => $fileInfo )
        {
            if ( $fileInfo[ 'size' ] > $this->maxFileSize )
            {
                $aResultCode   = 1;
                $this->errSize = $fileInfo[ 'size' ];
                return false;
            }
            
            list( $imgWidth, $imgHeight, $imgType ) = getimagesize( $fileInfo[ 'tmp_name' ] );
            
            if (   $imgType == 1 && !in_array( 'gif', $this->allowedTypes )
                || $imgType == 2 && !in_array( 'jpg', $this->allowedTypes )
                || $imgType == 3 && !in_array( 'png', $this->allowedTypes ) )
            {
                $aResultCode = 2;
                return false;
            }
            
            if ( $fileInfo[ 'error' ] != 0 )
            {
                $aResultCode = 3;
                return false;
            }
            
            $fileInfo[ 'type' ]   = $imgType;
            $fileInfo[ 'width' ]  = $imgWidth;
            $fileInfo[ 'height' ] = $imgHeight;
            
            $this->files[ $fileId ] = $fileInfo;
        }
        
        reset( $this->files );
        
        return true;
    }
    
    // resize
    function resize( $aThumbWidth, $aFileSuffix )
    {
        $max_width = $aThumbWidth;

        //Check if GD extension is loaded
/*        if (!extension_loaded('gd') && !extension_loaded('gd2'))
        {
            trigger_error("GD is not loaded", E_USER_WARNING);
            return false;
        }
*/
        reset( $this->files );
        foreach( $this->files as $fileId => $fileInfo )
        {
            $imgType  = $fileInfo[ 'type' ];
            $fileName = $fileInfo[ 'tmp_name' ];
            
            if ( $fileInfo[ 'height' ] <=  $aThumbWidth && $fileInfo[ 'width' ] <= $aThumbWidth )
            {
                $fileInfo[ 'resized' ][ $aFileSuffix ] = $fileName;
                $this->files[ $fileId ] = $fileInfo;
                continue;
            }
            
            $flTransparent = true;
            
            if ( $imgType == 1 )
            {
                $im = imagecreatefromgif( $fileName );
            }
            else if ( $imgType == 2 )
            {
                $flTransparent = false;
                $im = imagecreatefromjpeg( $fileName );
            }
            else if ( $imgType == 3 )
            {
                $im = imagecreatefrompng( $fileName );
            }
            
            /*** calculate the aspect ratio ***/
            $aspect_ratio = (float) $fileInfo[ 'height' ] / $fileInfo[ 'width' ];

            /*** calulate the thumbnail width based on the height ***/
            $thumb_height = round( $aThumbWidth * $aspect_ratio );

            while( $thumb_height > $max_width )
            {
                $aThumbWidth -= 1;
                $thumb_height = round( $aThumbWidth * $aspect_ratio );
            }
           
            $newImg = imagecreatetruecolor( $aThumbWidth, $thumb_height );
           
            /* Check if this image is PNG or GIF, then set if Transparent*/ 
            if( $flTransparent )
            {
                imagealphablending( $newImg, false );
                imagesavealpha( $newImg, true );
                $transparent = imagecolorallocatealpha( $newImg, 255, 255, 255, 127 );
                imagefilledrectangle( $newImg, 0, 0, $aThumbWidth, $thumb_height, $transparent );
            }
            imagecopyresampled( $newImg, $im, 0, 0, 0, 0, $aThumbWidth, $thumb_height, $fileInfo[ 'width' ], $fileInfo[ 'height' ] );
           
            //Generate the file, and rename it to $newFileName
            $resizedFileName = $fileName.'_'.$aFileSuffix;
            $fileInfo[ 'resized' ][ $aFileSuffix ] = $resizedFileName;
            
            if ( $imgType == 1 )
            {
                imagegif( $newImg, $resizedFileName );
            }
            else if ( $imgType == 2 )
            {
                imagejpeg( $newImg, $resizedFileName );
            }
            else if ( $imgType == 3 )
            {
                imagepng( $newImg, $resizedFileName );
            }
            
            $this->files[ $fileId ] = $fileInfo;
        }
        
        reset( $this->files );
    }
}

?>